<?php

namespace app\admin\model;

use app\common\model\TimeModel;

/**
 * Class User
 * @package app\admin\model
 * User : Orange Tel: 181-3414-1353 QQ:1139552211
 * Date    : 2021/6/10 上午11:12
 */
class User extends TimeModel {

    protected $name = "user";

    protected $deleteTime = "delete_time";

    protected $append = ['is_real_text'];
    /**
     * 获取关联用户等级
     * @return \think\model\relation\BelongsTo
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/10 上午11:12
     */
    public function userLevel() {
        return $this->belongsTo('\app\admin\model\UserLevel', 'level_id', 'id');
    }

    public function getIsRealTextAttr($value,$data) {
        if(empty($data['is_real'])){
            return '';
        }
        if($data['is_real'] == 0){
            $text = '未实名';
        }else if($data['is_real'] == 2){
            $text = '未审核';
        }else{
            $text = '已实名';
        }
        return $text;
    }

    /**
     * 获取用户等级
     * @return array
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/10 上午11:13
     */
    public function getUserLevelList() {
        return \app\admin\model\UserLevel::column('level', 'id');
    }

    /**
     * 获取状态码
     * @return string[]
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/10 上午11:14
     */
    public function getStatusList() {
        return ['0' => '禁用', '1' => '启用',];
    }

    /**
     *指定条数获取分割用户组
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User    : QingYang
     * Tel     : 181-3414-1353
     * QQ      : 1139552211
     * Date    : 2021/6/1 下午11:46
     */
    public function getUserChunk($limit = 5000) {
        //获取所有正常用户总数
        $count = self::instance()->where(['status' => 1])->cache(true)->count();
        $list  = self::instance()->where(['status' => 1])->cache(true)->select()->toArray();
        if ($count > $limit) {
            return array_chunk($list, $limit);
        } else {
            return $list;
        }
    }

    /**
     * 获取所有返佣用户及金额
     * @param $list
     * @return mixed
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/10 上午11:44
     */
    public function getAllReBateUser($list) {
        foreach ($list as $key => $value) {
            $pid           = self::instance()->where(['id' => $value['uid']])->column('superior')['superior'];
            $items[]['id'] = $pid;
            if ($value['type'] === Order::ORDER_TG) {
                $condition = explode(",", UserLevel::instance()->where(['user_id' => $pid])->column('node_hosting_ratio')['node_hosting_ratio']);
            } else {
                $condition = explode(",", UserLevel::instance()->where(['user_id' => $pid])->column('node_power_ratio')['node_power_ratio']);
            }
            $items[]['money'] = $condition[0] * $list['produce'] / 100;
            $items[]['type']  = $value['produce_type'];
        }

        return $items;
    }


}